package org.lql.algo.codecrush.week008;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author: liangqinglong
 * @date: 2025-08-19 00:05
 * @description: 面试题 16.21. 交换和 <a href="https://leetcode.cn/problems/sum-swap-lcci/description/">...</a>
 **/
public class FindSwapValues {

	public int[] findSwapValues(int[] array1, int[] array2) {
		int sum1 = 0, sum2 = 0;
		for (int k : array1) {
			sum1 += k;
		}
		for (int k : array2) {
			sum2 += k;
		}
		int diff = sum1 - sum2;
		// 如果 diff 不是偶数，无法平分
		if ((diff & 1) != 0) {
			return new int[]{};
		}
		int target = diff / 2; // 需要满足 a - b = target

		Set<Integer> set = new HashSet<>();
		for (int a : array1) {
			set.add(a);
		}

		for (int b : array2) {
			int a = b + target;
			if (set.contains(a)) {
				return new int[]{a, b};
			}
		}
		return new int[]{};
	}

	public static void main(String[] args) {
		FindSwapValues findSwapValues = new FindSwapValues();
		System.out.println(Arrays.toString(findSwapValues.findSwapValues(new int[]{1, 2, 3}, new int[]{2, 4, 6})));
		System.out.println(Arrays.toString(findSwapValues.findSwapValues(new int[]{1, 2, 3}, new int[]{4, 6, 8})));
		System.out.println(Arrays.toString(findSwapValues.findSwapValues(new int[]{1, 2, 3}, new int[]{4, 5, 6})));
		System.out.println(Arrays.toString(findSwapValues.findSwapValues(new int[]{4, 1, 2, 1, 1, 2}, new int[]{3, 6, 3, 3})));
	}
}
